package ctxdata

import (
	"context"
	"encoding/json"
	"fmt"

	"github.com/zeromicro/go-zero/core/logx"
)

// CtxKeyJwtUserId get uid from ctx
const CtxKeyJwtUserId = "jwtUserId"

func GetUidFromCtx(ctx context.Context) (uid int64, getUidErr error) {
	if jsonUid, ok := ctx.Value(CtxKeyJwtUserId).(json.Number); ok {
		if int64Uid, err := jsonUid.Int64(); err == nil {
			uid = int64Uid
		} else {
			logx.WithContext(ctx).Errorf(("GetUidFromCtx err : %+v"), err)
			getUidErr = fmt.Errorf("GetUidFromCtx error: %w", err)
		}
	} else {
		getUidErr = fmt.Errorf("GetUidFromCtx error: convert json.Number error")
	}
	return
}
